﻿namespace PageTypeBuilderUI.CustomProperties
{
    using System;
    using Configuration;
    using EPiServer.Core;
    using EPiServer.PlugIn;

    [Serializable]
    [PageDefinitionTypePlugIn(DisplayName = "PropertyGroupProperty", Description = "Property used for rendering property groups")]
    public class PropertyGroupProperty : PropertyString
    {

        public override IPropertyControl CreatePropertyControl()
        {
            PageTypeBuilderUIConfiguration configuration = PageTypeBuilderUIConfiguration.GetConfiguration();
            Type customPropertyGroupRendingControlType = System.Type.GetType(configuration.CustomRenderingControlType);

            if (customPropertyGroupRendingControlType == null)
            {
                throw new Exception(string.Format("The custom property group rendering control type '{0}' could not be loaded.", 
                    configuration.CustomRenderingControlType));
            }

            IPropertyControl control = Activator.CreateInstance(customPropertyGroupRendingControlType) as IPropertyControl;

            if (control == null)
            {
                throw new Exception(string.Format("The custom property group rendering control type '{0}' does not implement {1}.",
                    configuration.CustomRenderingControlType, typeof(IPropertyControl).FullName));
            }

            return control;
        }
    }
}
